--行会冠名地图功能
--#include "data\config\guild\GuildOwnSceneConfig.lua" once
--#include "data\functions\Guild\GuildMainBuilding.lua" once  	--行会主殿

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
取最后一击玩家的行会
GuildOwnScene001= "热烈祝贺%s的%s给予%s致命一击，%s行会获得%s的占领权。",
]]
function OnGuildOwnSceneBossDie( monster, sysarg, bossId, lastHitKiller )
	--print("OnGuildOwnSceneBossDie, bossId="..bossId)
	local ownSceneCfg = GuildOwnSceneCfg[bossId]
	if not ownSceneCfg then
	    return
	end
	
	if not CheckOpenServerDay( ownSceneCfg ) then 			--检查开服天数
		return
	end

	local guildId 	 = Actor.getGuildId(lastHitKiller)
	--print("OnGuildOwnSceneBossDie, guildId="..guildId)
	local guildLevel = GetGuildLevel(guildId)
	if guildId <= 0 or guildLevel <= 0 then				--没有行会
		return
	end

	for i, sceneId in ipairs( ownSceneCfg.scenes ) do
		local scenePtr = System.getScenePtrBySceneId( sceneId )
		if scenePtr then
			local oldGuildId = System.getSceneOwnGuild( scenePtr )
			System.setSceneOwnGuild( scenePtr, guildId )
			System.setSceneOwnGuildKillMonExpRate( scenePtr, ownSceneCfg.killMonExpRate )
		end
	end

	local guildName = Guild.getGuildName(guildId)
	local actorName = Actor.getName(lastHitKiller)
	local bossName	= MonsterNameDeleteNumber( bossId )
	local msg = string.format( Lang.ScriptTips.GuildOwnScene001, guildName, actorName, bossName, guildName, ownSceneCfg.mapName )
	System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )

	BroadGuildOwnScene() 			--全服广播
end


--[[
下发行会占领的场景数据（冠名）
客户端点开冠名地图地图的面板时，请求1次
]]
function SendGuildOwnSceneData( sysarg )
	local ownScenes = GetGuildOwnSceneData()
	DoSendGuildOwnSceneData( sysarg, ownScenes )
end

function GetGuildOwnSceneData()
	local ownScenes = {}
	for bossId, ownSceneCfg in pairs( GuildOwnSceneCfg ) do
		for i, sceneId in ipairs( ownSceneCfg.scenes ) do
			local scenePtr = System.getScenePtrBySceneId( sceneId )
			if scenePtr then
				local guildId = System.getSceneOwnGuild( scenePtr ) or 0
				table.insert( ownScenes, { sceneId, guildId } )
			end
		end
	end
	return ownScenes
end

--[[
下发行会占领的场景数据（冠名）
客户端点开冠名地图地图的面板时，请求1次
]]
function DoSendGuildOwnSceneData( sysarg, ownScenes )
	local netPack = DataPack.allocPacket(sysarg, 10, 54)		--enGuildSystemID, sSendGuildOwnScene
	if netPack then
		DataPack.writeByte(netPack, #ownScenes)
		for i, ownScene in ipairs( ownScenes ) do
			local sceneId = ownScene[1]
			local guildId = ownScene[2]
			DataPack.writeInt(netPack, sceneId)
			DataPack.writeInt(netPack, guildId)
			--print("SendGuildOwnSceneData, sceneId="..sceneId..", guildId="..guildId)
		end
		DataPack.flush(netPack)
	end
end



--[[
获取本人行会的冠名场景
]]
function GetGuildOwnSceneByGuildId( guildId )
	if guildId <= 0 then
		return nil
	end
	local guildLevel = GetGuildLevel(guildId)
	if guildLevel <= 0 then				--没有行会
		return nil
	end

	local ownScenes = {}
	for bossId, ownSceneCfg in pairs( GuildOwnSceneCfg ) do
		for i, sceneId in ipairs( ownSceneCfg.scenes ) do
			local scenePtr = System.getScenePtrBySceneId( sceneId )
			if scenePtr then
				local ownGuildId = System.getSceneOwnGuild( scenePtr ) or 0
				if ownGuildId > 0 and ownGuildId == guildId then
					table.insert( ownScenes, sceneId )
				end
			end
		end
	end
	return ownScenes
end

--[[
判断场景是否某行会的冠名地图（领地）
]]
function IsGuildOwnScene( guildId, sceneId )
	if guildId <= 0 then
		return false
	end
	local guildLevel = GetGuildLevel(guildId)
	if guildLevel <= 0 then				--没有行会
		return false
	end

	local scenePtr = System.getScenePtrBySceneId( sceneId )
	if scenePtr then
		local ownGuildId = System.getSceneOwnGuild( scenePtr ) or 0
		if ownGuildId > 0 and ownGuildId == guildId then
			return true
		end
	end
	return false
end

--[[
通知所有人冠名地图归属变化
]]
function BroadGuildOwnScene()
	--print("BroadGuildOwnScene...")
	local ownScenes 	= GetGuildOwnSceneData()
	local playerList 	= LuaHelp.getAllActorList() or {}
	for k, player in ipairs(playerList or {}) do
		DoSendGuildOwnSceneData( player, ownScenes )
	end
end
